package com.cy.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.cy.bean.GameList;
import com.cy.bean.UserList;
import com.cy.service.AboutMeService;
import com.cy.service.AboutMeServiceImpl;

/**
 * Servlet implementation class AboutMeServlet
 */
@WebServlet("/aboutme")
public class AboutMeServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setCharacterEncoding("utf-8");
		request.setCharacterEncoding("utf-8");
		AboutMeService aboutMeService = new AboutMeServiceImpl();
		String flag = request.getParameter("flag");
		String page = request.getParameter("page");
		int userid = ((UserList) request.getSession().getAttribute("gengraluser")).getId();
		UserList user = aboutMeService.findAllById(userid);
		request.setAttribute("nowloginuser", user);
		request.setAttribute("flag", flag);

		// 当没搜索时查找总条目数
		int totalNum = aboutMeService.findTotal(userid);
		if (page == null || page.equals("")) {
			page = "0";
		}
		List<GameList> mygames = aboutMeService.findAllByPage(Integer.parseInt(page), userid);
		request.setAttribute("mygames", mygames);
		int totalPage = (totalNum % 8 == 0) ? totalNum / 8 : totalNum / 8 + 1;
		// 将总页数与当前页数传出去
		request.setAttribute("totalPage", totalPage);
		request.setAttribute("page", Integer.parseInt(page));
		request.setAttribute("flag", flag);

		request.setAttribute("mainRight", "/WEB-INF/jsp/aboutme.jsp");
		request.getRequestDispatcher("/WEB-INF/jsp/main.jsp").forward(request, response);

	}

}
